package com.zt.teacher.listener;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.zt.common.pojo.ActiveClass;
import com.zt.teacher.netty.MyTextWebSocketFrameHandler;
import com.zt.teacher.service.ActiveClassService;
import com.zt.teacher.service.ClassSelectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * description:
 * author:zt
 * date:2022-01-08
 */
@Component
public class RedisKeyExpirationListener  extends KeyExpirationEventMessageListener {
    private static final Log log = LogFactory.get();

    @Autowired
    private ActiveClassService activeClassService;
    @Autowired
    private ClassSelectionService classSelectionService;
    @Autowired
    private MyTextWebSocketFrameHandler handler;

    public RedisKeyExpirationListener (RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {

        String key = message.toString();

        //表示选课结束 进行抽签筛选选课成功的学生
        if (key.endsWith("-selectClass")){
            String semester = key.substring(0,11);
            log.info("选课结束学期为：" + semester);
            //获取该学期的所有选修通选课堂
            List<ActiveClass> classes = activeClassService.getElectiveClassBySemester(semester);
            for (ActiveClass activeClass : classes) {
                //根据课堂id获取选择该节课的学生id
                List<String> studentIds = classSelectionService.getStuIdByClassId(activeClass.getId());
                //仅当选择该节课堂的学生人数多于该节课堂可选的学生人数时 需要进行抽签 删除部分选课学生
                if (studentIds.size() > activeClass.getStudentNumber()){
                    log.info("需要抽签class：" + activeClass);
                    HashSet<String> set = new HashSet<>();
                    //set当中存放需要删除的studentId，set长度为选择多出的人数
                    while (set.size() < studentIds.size() - activeClass.getStudentNumber()){
                        String id = RandomUtil.randomEle(studentIds);
                        set.add(id);
                        log.info("学生id：" + id);
                    }
                    classSelectionService.delete(new ArrayList<>(set),activeClass.getId());
                }
                else if (studentIds.size() < activeClass.getStudentNumber()){
                    //修改班级人数为实际选择人数
                    activeClass.setStudentNumber(studentIds.size());
                    activeClassService.update(activeClass);
                }
            }
        }
        //老师开启课堂未关闭 三小时后自动结束上课
        else if (key.endsWith("正在上课")){
            int classId = Integer.parseInt(key.substring(0, key.length() - 4));
            //移除班级group和teach
            handler.removeClass(classId);
        }
    }
}
